using System;
using System.Collections.Generic;

using Csla;

using EntModApp.EntServices.Business;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  public partial class CslaRoleInfo : 
    ReadOnlyBase<CslaRoleInfo>,
    IRole
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> RoleIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("RoleId"));
    public Guid RoleId 
    { 
      get
      {
        return ReadProperty(RoleIdPropertyInfo);
      }
      private set
      {
        LoadProperty(RoleIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> NamePropertyInfo = RegisterProperty(new PropertyInfo<string>("Name"));
    public string Name 
    { 
      get
      {
        return ReadProperty(NamePropertyInfo);
      }
      private set
      {
        LoadProperty(NamePropertyInfo, value);
      }
    }

    public static PropertyInfo<bool> IsAdminPropertyInfo = RegisterProperty(new PropertyInfo<bool>("IsAdmin"));
    public bool IsAdmin 
    { 
      get
      {
        return ReadProperty(IsAdminPropertyInfo);
      }
      private set
      {
        LoadProperty(IsAdminPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSecurableActionInfoCollection> SecurableActionsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSecurableActionInfoCollection>("SecurableActions"));
    public CslaSecurableActionInfoCollection SecurableActions 
    { 
      get
      {
        return ReadProperty(SecurableActionsPropertyInfo);
      }
      private set
      {
        LoadProperty(SecurableActionsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaUserInfoCollection> UsersPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserInfoCollection>("Users"));
    public CslaUserInfoCollection Users 
    { 
      get
      {
        return ReadProperty(UsersPropertyInfo);
      }
      private set
      {
        LoadProperty(UsersPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Factory Methods ]
    protected internal static CslaRoleInfo GetRoleInfo(Role data, RoleLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaRoleInfo result = new CslaRoleInfo();
      result.FetchRole(data, loadSettings);
      return result;
    }

    private static int ForceInitField = 0;
    protected CslaRoleInfo() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]
    protected void FetchRole(Role data, RoleLoadSettings loadSettings)
    {
      FetchRoleData(data, loadSettings);
      FetchRoleChildren(data, loadSettings);
    }

    protected void FetchRoleData(Role data, RoleLoadSettings loadSettings)
    {
      this.RoleId = data.RoleId;
      this.Name = data.Name;
      this.IsAdmin = data.IsAdmin;
    }

    protected void FetchRoleChildren(Role data, RoleLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeSecurableActions)
        this.SecurableActions = CslaSecurableActionInfoCollection.GetSecurableActionInfoCollection(data.SecurableActions, loadSettings.Inclusion.IncludedSecurableActionsSettings);
      if (loadSettings.Inclusion.IncludeUsers)
        this.Users = CslaUserInfoCollection.GetUserInfoCollection(data.Users, loadSettings.Inclusion.IncludedUsersSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IRole implementation ]

    Guid IRole.RoleId
    {
      get { return this.RoleId; }
    }

    string IRole.Name
    {
      get { return this.Name; }
      set
      {
        throw new NotSupportedException();
      }
    }

    bool IRole.IsAdmin
    {
      get { return this.IsAdmin; }
      set
      {
        throw new NotSupportedException();
      }
    }

    IBusinessObjectCollection<ISecurableAction> IRole.SecurableActions
    { 
      get
      {
        return this.SecurableActions;
      }
    }

    IBusinessObjectCollection<IUser> IRole.Users
    { 
      get
      {
        return this.Users;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return true; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return false; }
    }

    #endregion [ Explicit IRole implementation ]
  }
}
